<style scoped>
.layui-row {
    overflow: hidden;
}

.select-item {
    width: 34.333333%;
    float: right;
}

.layui-row:after,
.layui-row:before {
    clear: none;
}

.layui-form>>>.layui-form-select {
    z-index: inherit;
}
</style>

<template>
<div class="organization">
    <template v-if="permission.permis.indexOf(41) > -1">
    <div>
        <a href="javascript:;" class="layui-btn" @click="add"><i class="layui-icon">&#xe654;</i>添加订单</a>
    </div>
    </template>
    <form class="layui-form ">
        <div class="layui-col-lg4 select-item" style="text-align: right;width: auto;margin-top:20px;">

            <div class="layui-inline" style="width: 160px">
                <select type="text" name="type" id="type" class="layui-select">
                    <option value="">请选择种植户类型</option>
                    <option v-for="item in types" :value="item.dictkey">{{item.dicttext}}</option>
                </select>
            </div>
            <div class="layui-inline" style="width: 160px">
                <select type="text" name="farmerid" id="farmerid" class="layui-select" lay-search>
                    <option value="">请选择种植户</option>
                    <option v-for="item in farmers" :value="item.userid">{{item.name}}</option>
                </select>
            </div>
            <div class="layui-inline" style="width: 160px">
                <select type="text" name="batch" id="batch" class="layui-select">
                    <option value="">请选择批次</option>
                    <option v-for="item in batchs" :value="item.guid">{{item.name}}</option>
                </select>
            </div>
            <div class="layui-inline" style="width: 160px">
                <select type="text" name="ownername" id="ownername" class="layui-select" lay-search>
                    <option value="">请选择负责人</option>
                    <option v-for="item in owners" :value="item.realname">{{item.realname}}</option>
                </select>
            </div>
            <div class="layui-inline" style="width: 160px">
                <select name="status" id="status" style="width:250px">
                    <option value="">请选择状态</option>
                    <option value="1">正常</option>
                    <option value="2">禁用</option>
                </select>
            </div>
            <button type="button" class="layui-btn sou" @click="search">搜索</button>
            <button type="button" class="layui-btn sou" @click="exportExcel">导出excel</button>
        </div>
    </form>
    <table class="layui-table layui-form">
        <thead>
            <tr>
                <th>种植户</th>
                <th>类型</th>
                <th>电话</th>
                <th>自有仓库</th>
                <th>订单年份</th>
                <th>批次</th>
                <th>种植面积</th>
                <th>种植品种</th>
                <th>订单负责人</th>
                <th>种薯数量</th>
                <th>农药数量</th>
                <th>化肥数量</th>
                <th>状态</th>
<!--                <th>创建时间</th>-->
                <template v-if="permission.permis.indexOf(41) > -1">
                <th>操作</th>
                </template>
            </tr>
        </thead>
        <tbody>
            <tr class="id9" v-for="(val, index) in listdata" :key="index">
                <td style="max-width: 228px;">{{val.farmer}}</td>
                <td>{{val.typename}}</td>
                <td>{{val.telphone}}</td>
                <td>{{(val.storecapcity == 0 || val.storecapcity == '') ? '无' : '有'}}</td>
                <td>{{val.yearoforder}}</td>
                <td>{{val.batchname}}</td>
                <td>{{val.orderarea}} 亩</td>
                <td>{{val.productname}}</td>
                <td>{{val.orderownername}}</td>
                <td>{{val.seedweight}}</td>
                <td>{{val.pestweight}}</td>
                <td>{{val.fertweight}}</td>
                <template v-if="permission.permis.indexOf(41) > -1">
                <td><template v-if="val.status == 1">正常</template><template v-if="val.status == 2">禁用</template></td>
                </template>
<!--                <td>{{val.createtime}}</td>-->
                <td><a href="javascript:;" @click="edit(val.guid)">修改</a> | <a href="javascript:;" @click="del(val.guid)">删除</a></td>
            </tr>
        </tbody>
    </table>

    <Pagination :cur="currentPage" :all="totalPage" :callback="turnPage"></Pagination>
    <div style="height: 200px"></div>
</div>
</template>

<script>
import Pagination from '../common/Pagination.vue'
let qs = require('qs');

export default {
    name: 'FarmerOrder',
    components: {
        Pagination
    },
    data() {
        return {
            postData: {
                functiontype: 24,
                pageNo: 1,
                pageSize: 10
            },
            listdata: [],
            currentPage: 1,
            totalPage: 0,
            searchName: '',
            batchs: [],
            farmers: [],
            owners: [],
            types: [],
            permission: JSON.parse(sessionStorage.getItem('permissions')),
        }
    },
    mounted() {
        let that = this;
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer;
            that.getPageData();
            //获取种植户
            that.getFramers();
            that.getTypes();
            that.getBatchs();
            //获取订单签订负责人
            that.getUsers(2);
        });
    },
    updated: function () {
        layui.form.render('select');
        layui.form.render();
    },
    methods: {
        getFramers: function () {
            let that = this;
            this.axios({
                method: 'get',
                url: '/farmer/Farmerorder!getHasPermissionFarmers.action',
            }).then(response => {
                if (response.data.msg == "success") {
                    that.farmers = response.data.farmers;
                }  else {
                    this.toastText('获取种植户数据失败');
                }
            });
        },
        getUsers: function (type) {
            let that = this;
            let postParam = {};
            postParam['entity.userType'] = type;
            this.axios({
                method: 'get',
                url: '/system/Tuser!getUserAll.action',
                params: postParam
            }).then(response => {
                if (response.data.info != undefined && response.data.info.length > 0) {
                    that.owners = response.data.info;
                }  else {
                    this.toastText('获取负责人数据失败');
                }
            });
        },
        getTypes: function () {
            let that = this;
            let postParam = {};
            postParam['entity.dicttype'] = 73;
            this.axios({
                method: 'get',
                url: '/farmer/Dict!getDict.action',
                params: postParam
            }).then(response => {
                if (response.data.msg == "success") {
                    that.types = response.data.dicts;
                }  else {
                    this.toastText('获取种植户类型数据失败');
                }
            });
        },
        getBatchs: function () {
            let that = this;
            let postParam = {};
            postParam['functiontype'] = 24;
            this.axios({
                method: 'get',
                url: '/farmer/Farmerbatch!selectAll.action',
                params: postParam
            }).then(response => {
                if (response.data.msg == "success") {
                    that.batchs = response.data.result;
                } else if (response.data.msg == "fail") {
                    that.batchs = [];
                }  else {
                    this.toastText('获取批次数据失败');
                }
            });
        },
        search: function () {
            this.getPageData('search');
        },
        turnPage: function (data) {
            this.currentPage = data;
            this.getPageData();
        },
        getPageData: function (type = '') {
            let that = this;
            let loader = layui.layer.load();

            let postData = this.postData;
            if (type != 'search') {
                postData.pageNo = that.currentPage;
            } else {
                postData.pageNo = 1;
            }
            postData.type = $("#type").val();
            postData.farmerid = $("#farmerid").val();
            postData.orderbatchguid = $("#batch").val();
            postData.orderownername = $("#ownername").val();
            postData.status = $("#status").val();

            $.ajax({
                url: '/farmer/Farmerorder!list.action',
                type: "post",
                data: postData,
                dataType: "json",
                success: function (res) {
                    if (res.msg == 'success') {
                        //分页数据赋值
                        // that.currentPage = res.info.indexPage;
                        that.totalPage = res.pagelist.pageCount;
                        that.listdata = res.pagelist.rows;
                    } else {
                        that.alert("系统错误");
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });
        },
        add: function () {
            this.$parent.switchPage('AddFarmerOrder');
        },
        edit: function (id) {
            this.$parent.gid = id;
            this.$parent.switchPage('EditFarmerOrder');
        },
        del: function (id) {
            let that = this;
            layer.confirm('确定要删除该数据？', function () {
                let loader = layer.load();
                $.ajax({
                    url: '/farmer/Farmerorder!del.action',
                    type: "post",
                    data: {
                        guid: id
                    },
                    dataType: "json",
                    success: function (res) {
                        if (res.msg == "success") {
                            that.toast("删除成功");
                            that.getPageData();
                        } else {
                            that.toast('系统错误！');
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });

            })
        },
        exportExcel:function (){
            var that = this;
            let postData = this.postData;

            postData.pageNo = that.currentPage;
            postData.type = $("#type").val();
            postData.farmerid = $("#farmerid").val();
            postData.orderbatchguid = $("#batch").val();
            postData.orderownername = $("#ownername").val();
            postData.status = $("#status").val();

            window.location.href="/farmer/Farmerorder!exportOrderData.action?" + qs.stringify(postData);
        }
    }
}
</script>
